﻿using System;
using System.Web.Mvc;
using System.Text;
using System.Web.Mvc.Html;

public static class Helpers
{
    public static MvcHtmlString Calendar(this HtmlHelper html, int year, int month, DateTime selectedDate)
    {
        StringBuilder stringBuilder = new StringBuilder();
        TagBuilder tagBuilder = new TagBuilder("table");
        tagBuilder.AddCssClass("month-view");

        DateTime firstDayOfMonth = new DateTime(year, month, 1);
        DateTime previousMonth = firstDayOfMonth.AddDays(-1);
        int daysInPreviousMonth = DateTime.DaysInMonth(previousMonth.Year, previousMonth.Month);
        int numberOfDaysInMonth = DateTime.DaysInMonth(year, month);
        DateTime lastDayOfMonth = firstDayOfMonth.AddDays(numberOfDaysInMonth);
        DayOfWeek dayOfWeek = firstDayOfMonth.DayOfWeek;
        int numBlankCells = (int)dayOfWeek;

        int dayCounter = 1;     // We use this to know when to make a new row (%7)
        var dayNumber = 1;      // What day of the month are we on.

        stringBuilder.AppendLine("    <caption>" + firstDayOfMonth.ToString("MMMM") + ", " + firstDayOfMonth.ToString("yyyy") + "</caption>");
        stringBuilder.AppendLine("    <thead>");
        stringBuilder.AppendLine("        <tr>");
        stringBuilder.AppendLine("            <th scope=\"col\">Su</th>");
        stringBuilder.AppendLine("            <th scope=\"col\">Mo</th>");
        stringBuilder.AppendLine("            <th scope=\"col\">Tu</th>");
        stringBuilder.AppendLine("            <th scope=\"col\">We</th>");
        stringBuilder.AppendLine("            <th scope=\"col\">Th</th>");
        stringBuilder.AppendLine("            <th scope=\"col\">Fr</th>");
        stringBuilder.AppendLine("            <th scope=\"col\">Sa</th>");
        stringBuilder.AppendLine("        </tr>");
        stringBuilder.AppendLine("    </thead>");
        stringBuilder.AppendLine("    <tbody>");
        stringBuilder.AppendLine("        <tr>");
        
        // Print out all of the blanks so we start on the right day
        for (int i = numBlankCells - 1; i >= 0; i--, dayCounter++)
            stringBuilder.AppendLine("            <td class=\"inactive\" />");    

        // Go through every day in this month
        while (dayNumber <= numberOfDaysInMonth)
        { 
            string className = string.Empty;
            //if (dayNumber == DateTime.Now.Day && year == DateTime.Now.Year && month == DateTime.Now.Month) className = "today";
            if (dayNumber == selectedDate.Day && year == selectedDate.Year && month == selectedDate.Month) className = "today";

            stringBuilder.AppendLine("            <td class=\"" + className + "\">");
            stringBuilder.AppendLine("                " + LinkExtensions.ActionLink(html, dayNumber.ToString(), "Index", new { year = year, month = month, day = dayNumber }));
            stringBuilder.AppendLine("            </td>");
                
            dayCounter++;
            dayNumber++;

            // If we are at the end of the week, go to the next row
            if (dayCounter % 8 == 0 && dayNumber - 1 != numberOfDaysInMonth)
            { 
                stringBuilder.AppendLine("        </tr><tr>");
                dayCounter++;
            }
        }

        stringBuilder.AppendLine("        </tr>");
        stringBuilder.AppendLine("    </tbody>");

        tagBuilder.InnerHtml = stringBuilder.ToString();
        return new MvcHtmlString(tagBuilder.ToString(TagRenderMode.Normal));
    }
}